/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainPanel.java
 *
 * Created on May 24, 2009, 7:50:42 PM
 */

package matpred;

import bdl.UtilityBean;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author Demola
 */
public class MainPanel extends javax.swing.JFrame {

    /** Creates new form MainPanel */
    private MainPanel() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
            System.err.println(ex.toString());
        }

        initComponents();

        UtilityBean.ping();

        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension dim = tk.getScreenSize();
        setSize(dim);
        setExtendedState(JFrame.MAXIMIZED_BOTH);

        jTabbedPane1.addTab("Teams", TeamPanel.getTeamPanel());
        jTabbedPane1.addTab("Matches", MatchPanel.getMatchPanel());
        jTabbedPane1.addTab("Calendar", CalendarPanel.getCalendarPanel());
        jTabbedPane1.addTab("Search Prediction", SearchPanel.getSearchPanel());
        jTabbedPane1.addTab("Predicted Result", PastResults.getPastResult());
        jTabbedPane1.addTab("Ratings", RatingPanel.getRatingPanel());
    }

    public static MainPanel getMainPanel(){
        if(pnl == null){
            pnl = new MainPanel();
        }
        return pnl;
    }

    public void save(){
        CalendarPanel.getCalendarPanel().saveCalendar();
    }

    private boolean isSaved(){
        return CalendarPanel.getCalendarPanel().isSaved();
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Match Prediction");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jMenu1.setText("MatPred");

        jMenuItem1.setText("Ping Database");
        jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuItem1MousePressed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 692, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem1MousePressed
        boolean res = UtilityBean.ping();
        if(res){
            String msg = "Database is connected";
             JOptionPane.showMessageDialog(null, msg, "Successful", JOptionPane.INFORMATION_MESSAGE);
        }else{
            String msg = "Database not connected";
             JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jMenuItem1MousePressed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if(!isSaved()){
            // do you want to save before closing yes-no-cancel
            int opt = JOptionPane.showConfirmDialog(this, "Do you want to save the parameters");
            if((opt == JOptionPane.CANCEL_OPTION) || (opt == JOptionPane.CLOSED_OPTION)){
                return;
            }
            if(opt == JOptionPane.YES_OPTION){
                save();
            }
        }
        System.exit(0);
    }//GEN-LAST:event_formWindowClosing

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainPanel().setVisible(true);
            }
        });
    }

    private static MainPanel pnl;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

}




